home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / language / j62atari.zoo / io.c next >
C/C++ Source or Header  |  1993-02-25  |  5KB  |  159 lines

  1. /* ----------------------------------------------------------------------- */
  2. /* J-Source Version 6.2 - COPYRIGHT 1992 Iverson Software Inc.             */
  3. /* 33 Major Street, Toronto, Ontario, Canada, M5S 2K9, (416) 925 6096      */
  4. /*                                                                         */
  5. /* J-Source is provided "as is" without warranty of any kind.              */
  6. /*                                                                         */
  7. /* J-Source Version 6.2 license agreement:  You may use, copy, and         */
  8. /* modify the source.  You have a non-exclusive, royalty-free right        */
  9. /* to redistribute source and executable files.                            */
  10. /* ----------------------------------------------------------------------- */
  11. /*                                                                         */
  12. /* Input/Output                                                            */
  13.  
  14. #include "j.h"
  15.  
  16.  
  17. static void     sesmexit();
  18.  
  19. C               ibuf[2+NIBUF];
  20. I               jbrk=0;
  21. C               obuf[NOBUF];
  22.  
  23.  
  24. A inpl(e,n,s)B e;I n;C*s;{A z;C c;
  25.  jbrk=0;
  26.  if(n&&(c=*(s+n-1),CLF==c||CCR==c))--n;
  27.  if(n&&COFF==*(s+n-1))joff(zero);
  28.  ASSERT(5!=n||memcmp(s,"o\010u\010t",n),EVINPRUPT);
  29.  RZ(z=str(n,s)); s=(C*)AV(z);
  30.  if(e){jputs(qprompt); jputs(s); jputc(CNL);}
  31.  else if(outfile){fputs(s,outfile); fputc(CNL,outfile);}
  32.  R z;
  33. }
  34.  
  35. F1(joff){if(outfile&&outfile!=stdout)fclose(outfile); sesmexit(); exit(0);}
  36.  
  37. void jouts(s)C*s;{if(outfile)fputc(COUT,outfile); jputs(s);}
  38.  
  39. static void sigint(k)int k;{++jbrk; signal(SIGINT,sigint);}
  40.  
  41.  
  42. /* --------------------- Special Session Manager ------------------------- */
  43.  
  44. #if (!LINKJ && SYS & SYS_SESM)
  45.  
  46.  
  47. #if (SYS & SYS_PC386)
  48. C*edbuf;
  49. #endif
  50.  
  51. C breaker(){if(sesm)jstpoll(); if(jbrk){jbrk=0; jsignal(EVBREAK);} R!jerr;}
  52.  
  53. A jgets(){
  54.  if (sesm) {      jsti( (SI) NIBUF,ibuf);
  55.  } else {         RZ(fgets(ibuf,NIBUF,stdin));
  56.  }
  57.  R inpl(0,(I)strlen(ibuf),ibuf);
  58. }
  59.  
  60. void jputc(c)C c;{
  61.  if(tostdout){if(sesm)jsto(MTYOUT,(SI)1,&c); else fputc(c,stdout);}
  62.  if(outfile)fputc(c,outfile);
  63. }
  64.  
  65. void jputs(s)C*s;{
  66.  if(tostdout){if(sesm)jsto(MTYOUT,(SI)strlen(s),s); else fputs(s,stdout);}
  67.  if(outfile)fputs(s,outfile);
  68. }
  69.  
  70. void prompt(s)C*s;{
  71.  if(tostdout){if(sesm)jsto(MTYOIN,(SI)strlen(s),s); else fputs(s,stdout);}
  72.  if(outfile)fputs(s,outfile);
  73. }
  74.  
  75. static void sesmexit(){if(sesm)jststop();}
  76.  
  77. C sesminit(){
  78.  if(sesm){A t;I j,mask=0xfffffff0L;
  79.   struct{Ptr vlog;SI nlog;Ptr vinb;SI ninb;Ptr vfkd;SI nfkd;Ptr vedb;SI nedb;}in;
  80. #if (SYS & SYS_PC+SYS_PC386)
  81.   /* The 15+ and &0xfffffff0 are because addresses must be segment aligned */
  82.   GA(t,CHAR,15+NLOG,1,0); j=(I)(15+(C*)AV(t)); j&=mask; in.vlog=(Ptr)j; in.nlog=NLOG;
  83.   GA(t,CHAR,15+NTA ,1,0); j=(I)(15+(C*)AV(t)); j&=mask; in.vinb=(Ptr)j; in.ninb=NTA ;
  84.   GA(t,CHAR,15+NFKD,1,0); j=(I)(15+(C*)AV(t)); j&=mask; in.vfkd=(Ptr)j; in.nfkd=NFKD;
  85.   GA(t,CHAR,15+NEDB,1,0); j=(I)(15+(C*)AV(t)); j&=mask; in.vedb=(Ptr)j; in.nedb=NEDB;
  86. #endif
  87. #if (SYS & SYS_PC386)
  88.   edbuf=in.vedb;
  89. #endif
  90.   jstinit((Ptr)&in);
  91.  }
  92. #if (SYS & SYS_PC386)
  93.  else jstsbrk();
  94. #else
  95.  signal(SIGINT,sigint);
  96. #endif
  97.  R 1;
  98. }
  99.  
  100. C*wr(n,v)I n;C*v;{I k=0,m;
  101.  if(tostdout){
  102.   if(sesm)while(n>k&&!jerr){m=MIN(3000,n-k); jsto(MTYOUT,(SI)m,v+k); k+=m;}
  103.   else    while(n>k&&!jerr)k+=fwrite(v+k,sizeof(C),(size_t)(n-k),stdout);
  104.  }
  105.  if(outfile){
  106.   fputc(COUT,outfile);
  107.   k=0;    while(n>k&&!jerr)k+=fwrite(v+k,sizeof(C),(size_t)(n-k),outfile);
  108.  }
  109.  jputc(CNL); R v+n;
  110. }
  111.  
  112.  
  113. /* --------------------- Others (No Session Manager) --------------------- */
  114.  
  115. #else
  116.  
  117. C breaker(){R!jerr;}
  118.  
  119. /* Import the main routine from the EDITLINE library */
  120. extern char *readline (const char *prompt);
  121. extern void add_history(char *string);
  122. char *luxptr = NULL;
  123.  
  124. A jgets(){
  125.     char        *p;
  126. /*  RZ(fgets(ibuf,NIBUF,stdin)); R inpl(0,(I)strlen(ibuf),ibuf); */
  127.     p = readline(qprompt);
  128.     if ((strncmp(p, ")off", 4) == 0) /* Hack in the classic  APL )off command */
  129.         || (strncmp(p, ")OFF", 4) == 0)) {
  130.       free(p);        /* Throw away the command */
  131.       p = "\004\n";   /* Replace it... */
  132.     }
  133.     add_history(p);
  134.     strcpy(ibuf,p);
  135.     free(p);
  136.     R inpl(0,(I)strlen(ibuf),ibuf);
  137. }
  138.  
  139. void jputc(c)C c;{if(tostdout)fputc(c,stdout); if(outfile)fputc(c,outfile);}
  140.  
  141. void jputs(s)C*s;{if(tostdout)fputs(s,stdout); if(outfile)fputs(s,outfile);}
  142.  
  143. void prompt(s)C*s;{jputs(s);}
  144.  
  145. static void sesmexit(){}
  146.  
  147. C sesminit(){signal(SIGINT,sigint); R 1;}
  148.  
  149. C*wr(n,v)I n;C*v;{I k=0;
  150.  if(tostdout)while(n>k&&!jerr)k+=fwrite(v+k,sizeof(C),(size_t)(n-k),stdout);
  151.  if(outfile){
  152.   fputc(COUT,outfile);
  153.   k=0;       while(n>k&&!jerr)k+=fwrite(v+k,sizeof(C),(size_t)(n-k),outfile);
  154.  }
  155.  jputc(CNL); R v+n;
  156. }
  157.  
  158. #endif
  159.